﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Mvvm;
using System.Windows.Input;
using Prism.Commands;
using System.Diagnostics;
using ModuleBase;

namespace HostComputerPlatform.Models
{
    public class LogicTableRow : BindableBase
    {
        private ObservableCollection<EditableItem> _sourceModule = new();
        public ObservableCollection<EditableItem> SourceModule
        {
            get => _sourceModule;
            set => SetProperty(ref _sourceModule, value);
        }

        private ObservableCollection<EditableItem> _trueBranch = new();
        public ObservableCollection<EditableItem> TrueBranch
        {
            get => _trueBranch;
            set => SetProperty(ref _trueBranch, value);
        }

        private ObservableCollection<EditableItem> _falseBranch = new();
        public ObservableCollection<EditableItem> FalseBranch
        {
            get => _falseBranch;
            set => SetProperty(ref _falseBranch, value);
        }

        // 三级联动属性
        private string _selectedCategory;
        public string SelectedCategory
        {
            get => _selectedCategory;
            set { SetProperty(ref _selectedCategory, value); SelectedModuleType = null; SelectedInstanceId = null; }
        }
        private string _selectedModuleType;
        public string SelectedModuleType
        {
            get => _selectedModuleType;
            set { SetProperty(ref _selectedModuleType, value); SelectedInstanceId = null; }
        }
        private string _selectedInstanceId;
        public string SelectedInstanceId
        {
            get => _selectedInstanceId;
            set { SetProperty(ref _selectedInstanceId, value); }
        }
        // 过滤后的类型列表（需外部传入全局AvailableModules）
        public IEnumerable<string> GetFilteredModuleTypes(IEnumerable<IModuleBase> availableModules)
        {
            if (string.IsNullOrEmpty(SelectedCategory)) return Enumerable.Empty<string>();
            return availableModules.Where(m => m.Category == SelectedCategory).Select(m => m.Name).Distinct().ToList();
        }
        // 过滤后的实例列表+新增实例（需外部传入全局InstantiatedModules）
        public IEnumerable<object> GetFilteredInstancesWithNewOption(IEnumerable<IModuleBase> instantiatedModules)
        {
            if (string.IsNullOrEmpty(SelectedModuleType))
                return new[] { new { Name = "新增实例", Id = "新增实例" } };
            // 只显示每个别名的第一个实例
            var list = instantiatedModules
                .Where(m => m.TemplateName == SelectedModuleType && m.Category == SelectedCategory)
                .GroupBy(m => string.IsNullOrWhiteSpace(m.Alias) ? m.Name : m.Alias)
                .Select(g => g.First())
                .Select(m => new { Name = m.DisplayName, Id = m.Id })
                .ToList();
            list.Add(new { Name = "新增实例", Id = "新增实例" });
            return list;
        }

        // 添加一个方法来处理删除后的刷新
        public void RefreshAfterDelete()
        {
            // 强制属性变更通知
            RaisePropertyChanged(nameof(SourceModule));
            RaisePropertyChanged(nameof(TrueBranch));
            RaisePropertyChanged(nameof(FalseBranch));
        }
    }

    public class EditableItem : BindableBase
    {
        private string _content = string.Empty;
        public string Content
        {
            get => _content;
            set => SetProperty(ref _content, value);
        }

        public ICommand DeleteCommand { get; }

        public EditableItem(Action<EditableItem> deleteAction)
        {
            DeleteCommand = new DelegateCommand(() =>
            {
                deleteAction?.Invoke(this);
                Debug.WriteLine($"Item deleted. Content: {Content}");
            });
        }
    }
}
